package com.caizongzhen.www.controller;

import com.caizongzhen.www.dto.VerifyParaDto;
import com.caizongzhen.www.entity.Illness;
import com.caizongzhen.www.entity.Medicine;
import com.caizongzhen.www.entity.User;
import com.caizongzhen.www.service.MedicineService;
import com.caizongzhen.www.util.Assert;
import com.caizongzhen.www.util.RespResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
public class MedicalControll {
    @Autowired
    private MedicineService medicineService;


    /*
    * 添加、修改页面
    * */
    @RequestMapping("add-medical")
    public String addMedical(Model model,HttpSession session,Integer id){
        // 判断用户是否登录
        if(Assert.isEmpty((User)session.getAttribute("loginUser"))) return "redirect:/index";
        Medicine medicine = new Medicine();
        if (Assert.isEmpty(id)) model.addAttribute("medicine",medicine);
        else {
            medicine = medicineService.getById(id);
            model.addAttribute("medicine",medicine);
        }
        return "add-medical";
    }

    /*
    *  查找药品_搜索后页面
    * */
    @RequestMapping("/findMedicines")
    public String findMedicines(Model model,String nameValue) {
        List<Medicine> medicineList  = medicineService.findMedicineByName(nameValue);
        model.addAttribute("medicineList",medicineList);
        return  "illness";
    }

    /*
     * 药品详情页面
     * */
    @RequestMapping("/findMedicineOne")
    public String findMedicines(Model model,Integer id) {
        List<Medicine> medicineList = new ArrayList<>();
        if (!Assert.isEmpty(id)) {
            Medicine medicine =(Medicine) medicineService.getById(id);
            medicineList.add(medicine);
        }
        else {
            return "redirect:/index";
        }
        model.addAttribute("medicineList",medicineList);
        return  "illness";
    }

    /*
     * 药品管理页面
     * */
    @RequestMapping("/all-medical")
    public String allIMedical(Model model, @RequestParam(defaultValue = "1") Integer currentPage, @RequestParam(defaultValue = "10") Integer pageSize, HttpSession session) {
        // 判断是否登录
        User user =(User) session.getAttribute("loginUser");
        if (Assert.isEmpty(user)) return "redirect:/index";
        List<Medicine> allIllness = medicineService.findMedicineByName(null);
        model.addAttribute("medicines",allIllness);
        return  "all-medical";
    }

    /*
     * 添加、修改药瓶信息
     * */
    @RequestMapping("/medicine/save")
    @ResponseBody
    public RespResult medicineSave(Medicine medicine){
        VerifyParaDto verifyParaDto = new VerifyParaDto();
        verifyParaDto = judgeIlMedicineSave(medicine);
        if (!verifyParaDto.Tag) return RespResult.fail(verifyParaDto.Msg);
        if (Assert.isEmpty(medicine.getId())) {
            medicine.setCreateTime(new Date());
            medicine.setUpdateTime(new Date());
        }
        else medicine.setUpdateTime(new Date());
        boolean bool = medicineService.saveOrUpdate(medicine);
        if(bool) return RespResult.success("操作成功");
        else return RespResult.fail("操作失败");
    }


    /*
     * 删除药品信息
     * */
    @PostMapping("/medicine/delete")
    @ResponseBody
    public RespResult medicineDelete(Integer id, HttpSession session){
        if (Assert.isEmpty(id)) return RespResult.fail("id不能为空");
        Medicine medicine = medicineService.getById(id);
        if (Assert.isEmpty(medicine)) return RespResult.fail("传入的id有误");
        boolean bool = medicineService.removeById(id);
        if (bool) return RespResult.success("删除成功");
        else return RespResult.fail("删除失败");
    }



    /*
     *  判断新增、修改疾病是否合法
     * */
    private VerifyParaDto judgeIlMedicineSave(Medicine medicine) {
        VerifyParaDto verifyParaDto = new VerifyParaDto();
        verifyParaDto.Tag = false;
//        if (Assert.isEmpty(medicine.getImgPath())) verifyParaDto.Msg = "药品图片不能为空";
        if (Assert.isEmpty(medicine.getMedicineName())) verifyParaDto.Msg = "药瓶名称不能为空";
        else if (Assert.isEmpty(medicine.getKeyword())) verifyParaDto.Msg = "关键字不能为空";
        else if (Assert.isEmpty(medicine.getMedicineBrand())) verifyParaDto.Msg = "品牌不能为空";
        else if (Assert.isEmpty(medicine.getMedicinePrice())) verifyParaDto.Msg = "价格不能为空";
        else if (Assert.isEmpty(medicine.getMedicineEffect())) verifyParaDto.Msg = "功效不能为空";
        else if (Assert.isEmpty(medicine.getInteraction())) verifyParaDto.Msg = "禁忌不能为空";
        else if (Assert.isEmpty(medicine.getUsAge())) verifyParaDto.Msg = "用法不能为空";
        else if (Assert.isEmpty(medicine.getMedicineType())) verifyParaDto.Msg = "药物类型不能为空";
        else verifyParaDto.Tag = true;
        return verifyParaDto;
    }

}
